/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 pjoc.pub, blademainer.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package pub.pjoc.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author xiongyingqi
 * @since 16-9-9 上午10:32
 */
public abstract class HostUtils {
  private static final Logger logger = LoggerFactory.getLogger(HostUtils.class);

  public static InetAddress getInetAddress() {

    try {
      return InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
      System.out.println("unknown host!");
    }
    return null;

  }

  public static String getHostIp(InetAddress netAddress) {
    if (null == netAddress) {
      return null;
    }
    String ip = netAddress.getHostAddress(); //get the ip address
    return ip;
  }

  /**
   * Returns width machine number with the default max length.
   *
   * @return machine number
   * @see #maxMachineNumberLength()
   */
  public static String getWidthMachineNumber() {
    return getWidthMachineNumber(maxMachineNumberLength());
  }

  /**
   * Returns width machine number.
   *
   * @param minLength minimum length of machine number
   * @return machine number
   */
  public static String getWidthMachineNumber(int minLength) {
    String hostName = getHostName();
    if (hostName == null) {
      return null;
    }
    int hash = hostName.hashCode();
    long unsignedHash = getUnsignedInt(hash);
    String machineNo = unsignedHash + "";
    int least = minLength - machineNo.length();
    if (least > 0) {
      for (int i = 0; i < least; i++) {
        machineNo = "0" + machineNo;
      }
    }
    return machineNo;
  }

  /**
   * Returns a hash code for this string. The hash code for a
   * {@code String} object is computed as
   * <blockquote><pre>
   * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
   * </pre></blockquote>
   * using {@code int} arithmetic, where {@code s[i]} is the
   * <i>i</i>th character of the string, {@code n} is the length of
   * the string, and {@code ^} indicates exponentiation.
   * (The hash value of the empty string is zero.)
   *
   * @return a hash code value for this object.
   */
  public static int stringHashCode(String string) {
    int h = 0;
    char[] value = string.toCharArray();
    if (h == 0 && value.length > 0) {
      char val[] = value;

      for (int i = 0; i < value.length; i++) {
        h = 31 * h + val[i];
      }
    }
    return h;
  }

  /**
   * Default machine number length.
   *
   * @return max length
   */
  public static int maxMachineNumberLength() {
    return (Integer.MAX_VALUE + "").length();
  }

  public static String getHostName() {
    return getHostName(getInetAddress());
  }

  public static String getHostName(InetAddress netAddress) {
    if (null == netAddress) {
      return null;
    }
    String name = netAddress.getHostName(); //get the host address
    return name;
  }

  public static long getUnsignedInt(int x) {
    return x & 0x00000000ffffffffL;
  }

  public static void main(String[] args) {
    String hostName = getHostName(getInetAddress());
    System.out.println(hostName.hashCode());
    System.out.println(stringHashCode(hostName));
    System.out.println(Integer.MAX_VALUE);
    System.out.println(getWidthMachineNumber());
    System.out.println(getUnsignedInt(Integer.MIN_VALUE));
    System.out.println(getUnsignedInt(Integer.MIN_VALUE + 1));
  }

}
